package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.choose.HangyeBean;
import com.eqdd.yiqidian.bean.rebuild.WorkExpListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.bean.utils.MultiChooseBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.MultiChooseTestUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.ISelectAble;

/**
 * Created by 吕志豪 on 17-10-12  上午10:21.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class WorkExpInfoActivity extends SlimBaseMultiEditActivity {


    private RecyclerViewCustom dataBinding;
    private BottomDialog hangyeDialog;
    private ActionSheetDialog chooseGuimo;
    private ActionSheetDialog chooseXingzhi;
    private ActionSheetDialog chooseXinzi;
    private String code;
    private String msg;
    private WorkExpListResultBean bean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "添加工作经历");
        bean = (WorkExpListResultBean) getIntent().getSerializableExtra("bean");
        isSave = false;
        isRead = false;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                if (chooseGuimo == null) {
                    SingleChooseUtil.getChooseDialog(WorkExpInfoActivity.this, 4, (sheet) -> {
                        chooseGuimo = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(1).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(1);
                        updateWorkExp();
                    });
                } else {
                    chooseGuimo.show();
                }
                break;
            case 2:
                if (chooseXingzhi == null) {
                    SingleChooseUtil.getChooseDialog(WorkExpInfoActivity.this, 6, (sheet) -> {
                        chooseXingzhi = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(2).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(2);
                        updateWorkExp();

                    });
                } else {
                    chooseXingzhi.show();
                }
                break;
            case 5:
                if (chooseXinzi == null) {
                    SingleChooseUtil.getChooseDialog(WorkExpInfoActivity.this, 36, (sheet) -> {
                        chooseXinzi = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(5).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(5);
                        updateWorkExp();

                    });
                } else {
                    chooseXinzi.show();
                }
                break;
            case 6:
                if (hangyeDialog != null) {
                    hangyeDialog.show();
                }
                break;

        }
    }

    @Override
    protected void onChooseClick(int position) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.TOGGLE_WORK_EXP_OVERT)
                .params("userGuid", user.getGuid())
                .params("workepid", bean.getId())
                .params("isOvert", setParam(10).equals("不公开") ? 0 : 1)
                .execute(new DialogCallBack<HttpResult>(WorkExpInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            setResult(Config.CHANGE);
                        }
                    }
                });
    }

    @Override
    protected void onTimeClick(int position) {
        updateWorkExp();
    }

    @Override
    protected void setEditBeans() {

        initHangyePicker();
        datas.add(new SlimEditEnterBean("企业名称", bean.getCompany(), 0, "请填写"));
        datas.add(new SlimEditEnterBean("企业规模", bean.getEnterpriseScale(), 1, true, "请选择"));
        datas.add(new SlimEditEnterBean("企业性质", bean.getEnterpriseNature(), 2, true, "请选择"));
        datas.add(new SlimEditEnterBean("所在部门", bean.getDepartment(), 3, "请填写"));
        datas.add(new SlimEditEnterBean("担任职位", bean.getPost(), 4, "请填写"));
        datas.add(new SlimEditEnterBean("薪资范围", bean.getMonthlySalary(), 5, true, "请选择"));

        datas.add(new SlimEditEnterBean("工作行业", bean.getInduCategoryName(), 6, true, "请选择"));
        datas.add(new SlimEditTimeBean("工作开始时间", bean.getStartTime(), 7, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditTimeBean("工作结束时间", bean.getEndTime(), 8, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditEnterBean("工作描述", bean.getJobDescri(), 9, "请填写"));
        datas.add(new SlimEditChooseBean("是否公开", bean.isIsOvert() ? "公开" : "不公开", 10, "请选择", new String[]{"不公开", "公开"}));

    }

    private void initHangyePicker() {
        OkGo.<List<HangyeBean>>post(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .params("type", 2)
                .execute(new DialogCallBack<List<HangyeBean>>(WorkExpInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<List<HangyeBean>> response) {
                        List<HangyeBean> httpResult = response.body();

                        List<MultiChooseBean> multiChooseBeen = new ArrayList<>();
                        multiChooseBeen.addAll(httpResult);
                        hangyeDialog = new MultiChooseTestUtil().getAreaDialog(WorkExpInfoActivity.this, 3, multiChooseBeen, (d, a) -> {
                            hangyeDialog.dismiss();
                            StringBuilder result = new StringBuilder();
                            for (ISelectAble iSelectAble : a) {
                                result.append(iSelectAble.getName());
                            }
                            code = ((HangyeBean) (a.get(2).getArg())).getCode();
                            slimAdapterEx.getDataItem(6).setContent(a.get(2).getName());
                            slimAdapterEx.notifyItemChanged(6);
                            updateWorkExp();

                        });
                    }

                });

    }

    private void updateWorkExp() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_WORK_EXP)
                .params("userGuid", user.getGuid())
                .params("workepid", bean.getId())
                .params("startTime", setParam(7))
                .params("endTime", setParam(8))
                .params("company", setParam(0))
                .params("induCategoryCode", code == null ? bean.getInduCategoryCode() : code)
                .params("induCategoryName", setParam(6))
                .params("enterpriseNature", setParam(2))
                .params("enterpriseScale", setParam(1))
                .params("monthlySalary", setParam(5))
                .params("department", setParam(3))
                .params("post", setParam(4))
                .params("jobDescri", setParam(9))
                .execute(new DialogCallBack<HttpResult>(WorkExpInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                            setResult(Config.CHANGE);
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            updateWorkExp();
        }
    }
}
